package pri.zxx.learndemo.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Twilight
 * @desc 给定一个整数数组 nums 和一个目标值 target，请你在该数组中找出和为目标值的那 两个 整数，并返回他们的数组下标。
 * <p>
 * 你可以假设每种输入只会对应一个答案。但是，你不能重复利用这个数组中同样的元素。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/two-sum
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @createTime 2020-03-26-21:02
 */
public class SumTarget {

    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            for (int j = i + 1; j < length; j++) {
                if (nums[i] + nums[j] == target) {
                    result[0] = i;
                    result[1] = j;

                }
            }
        }
        return result;
    }

    public static Map<Integer, Integer> twoSumHash(int[] nums, int target) {
        Map<Integer, Integer> result = new HashMap<>();

        Map<Integer, Integer> hashtable = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            hashtable.put(nums[i], i);
        }

        for (int i = 0; i < nums.length; i++) {
            if (hashtable.containsKey(target - nums[i])) {
                result.put(hashtable.get(target - nums[i]), i);
            }
        }
        return result;
       /* for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }*/
//        return new int[0];
    }

    public static void main(String[] args) {
        int[] ints1 = {2, 3, 7, 11, 15, 16};
        int[] ints = twoSum(ints1, 18);
        System.out.println(Arrays.toString(ints));
        System.out.println(twoSumHash(ints1, 18));
    }

    public static int[] myTest(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            hashtable.put(nums[i], i);
        }

        for (int i = 0; i < nums.length; i++) {
            if (hashtable.containsKey(target - nums[i]) && (hashtable.get(target - nums[i]) != i)) {
                // result.put(hashtable.get(target - nums[i]), i);
                return new int[]{i, hashtable.get(target - nums[i])};
            }
        }
        return new int[0];
    }
}
